<template>
	<view>
		<view class="doctor_message">
			<view class="doctor_photo"></view>
			<h3>{{params.ksysmc}}</h3>
			<view style="margin-top: 15px;">
				<b>{{params.mzksmc}}</b>
				<span style="color: #A5A5A5;margin-left: 20px;">{{params.yssc00}}</span>
			</view>
		</view>
		<view class="medical_info">
			<ul>
				<li>就诊日期<b>{{params.ghrq}}</b></li>
				<li>就诊科室<b>{{params.mzksmc}}</b></li>
				<li @click="selectTime">就诊时段<b>{{params.ksjzsj}}-{{params.jsjzsj}}<view class="select_icon"></view></b></li>
				<li @click="selectPerson">就诊人<b>{{medicalData.xming0}}<view class="select_icon"></view></b></li>
				<li>就诊费用<b>{{params.ghzlf0}}元</b></li>
			</ul>
		</view>
		<button class="sure_btn" @click="sureRegistration">确认挂号</button>
		
		<personList styles="display:none;" ref="personList" v-on:checkedPerson= "getDatas"></personList>
	</view>
</template>

<script>
	import personList from "../../modules/personList.vue";
	import pay from "@/tool/pay.js";
	export default {
		data() {
			return {
				out_trade_no:"",
				showBox:false,
				params:{},
				doctorData:{},//医生信息
				medicalData:{},//患者信息
				personListData:[]
			}
		},
		components:{
				personList
			},
		onLoad(option) {
			if(option.item){
				var data=JSON.parse(option.item);
				console.log(data);
				this.params=data;
				let user=uni.getStorageSync("user");
				this.medicalData=user;
				this.out_trade_no="WXREG"+new Date().getTime();
				this.init();
			}
			
		},
		methods: {
			init(){
				if(this.params.sffsjd=="Y"){
					this.HisLockRegistration(true);
				}
				//this.HisRegistration(this.params);
			},
			selectTime(){
				if(!this.params.time){
					return;
				}
				var params=this.params;
				uni.redirectTo({
					url:"../appointmentTime/appointmentTime?id="+params.id+"&date="+params.date
				})
			},
			getDatas(item){
				this.medicalData=item;
			},
			selectPerson(){
				this.$refs.personList.selectPerson();
			},
			sureRegistration(){
				if(this.params.ghzlf0>0){
					var payJson={
						"out_trade_no": this.out_trade_no,//后台生成，唯一性
						"description": "挂号缴费",
						"notify_url": "http://127.0.0.1",//异步回调地址，后台配置
						"amount": {
							"total": (parseFloat(this.params.ghzlf0) *100),//(parseFloat(this.params.ghzlf0) *100
							"currency": "CNY"
						},
						"payer": {
							"openid": uni.getStorageSync("openid")
						}
					};
					pay.prePay(payJson,success=>{
						//这里应先进行预下单，然后进行锁号挂号操作
						this.HisRegistration(success);
					},error=>{
						//取消支付，支付失败等等 取消或更新失败状态
						console.log(error);
						uni.showToast({
							title:"支付失败",
							icon:"error"
						})
					})
				}else{
					//直接进行锁号挂号
					this.HisRegistration();
				}
			},
			HisLockRegistration(flag){
				var params=this.params;
				var user=uni.getStorageSync("user");
				var pdxh00="";
				if(params.hybh00){
					pdxh00=parseInt(params.hybh00)+1;
				}
				
				let lockData={
					"kh000":params.kh0000,
					"klx000":"3",
					"patid0":user.patid,
					"xming0":user.xming0,
					"ghrq00":params.ghrq,
					"mzksbh":params.mzksbh,
					"hybh00":params.hybh00,
					"pdxh00":pdxh00,
					"ghsj00":new Date(),
					"ywlsh0":""+new Date().getTime(),
					"funid":"F07.09.02.04"
				}
				if(params.ksysbh){
					lockData.ksysbh=params.ksysbh;//科室医生名称
				}
				uni.showLoading({
					mask:true,
					title:"请等待..."
				})
				 //锁号
				this.$postJson("/qnyesb/qnyesbproxy",lockData,res=>{
					if(res.state==0){
						//平台预支付，登记挂号
						//this.HisRegistration(res);
					}else{
						uni.showToast({
							title:res.message,
							icon:"none",
							duration:3000
						})
					}
					uni.hideLoading();
				})
			},
			HisRegistration(res){
				//登记挂号
				let params=this.params;
				let user=uni.getStorageSync("user");
				let ghlsh0="";
				if(res.data){
					ghlsh0=res.data.ghlsh0;
				}
				let date=new Date();
				let yyyy=date.getFullYear();
				let HH=date.getHours()+1;
				if(HH<10){
					HH="0"+HH;
				}
				let mm=date.getMinutes();
				if(mm<10){
					mm="0"+mm;
				}
				let ss=date.getSeconds();
				if(ss<10){
					ss="0"+ss;
				}
				let ghsj00=params.ghrq+" "+HH+":"+mm+":"+ss;
				//let 
				let registerData={
					"kh0000":user.sfzh00,
					"klx000":"3",
					"patid0":user.patid0,
					"xming0":user.xming0,
					"ghrq00":params.ghrq,
					"mzksbh":params.mzksbh,
					"mzksmc":params.mzksmc,
					"ghsj00":ghsj00,//挂号时间
					"ghsjqj":params.ghsjqj,
					"hybh00":params.hybh00,//号源编号
					"ghlsh0":ghlsh0,//
					"xtmc00":"3",//系统名称
					"zffs00":"2",//支付方式
					"hzsfbh":"0",
					"ddh000":this.out_trade_no,
					// "jsddh0":this.out_trade_no,
					"yhjyxx":JSON.stringify(JSON.stringify(res)),
					"ghfy00":params.ghzlf0,
					"ywlsh0":""+new Date().getTime(),
					"funid":"F07.09.02.05"
				}
				if(params.ksysmc){
					registerData.ksysmc=params.ksysmc;//科室医生名称
				}
				if(params.ksysbh){
					registerData.ksysbh=params.ksysbh;//科室医生名称
				}
				uni.showLoading({
					title:"登记挂号中...",
					mask:true
				})
				this.$postJson("/qnyesb/qnyesbproxy",registerData,res1=>{
					if(res1.state==0||res1.state=="0"){
						uni.showModal({
							title:"温馨提示",
							content:"挂号成功,排队序号为："+res1.data.pdxh00+",是否现在返回首页",
							success(showRes) {
								if(showRes.confirm){
									uni.switchTab({
										url:"../index/index"
									})
								}
							}
						})
					}else{
						uni.showModal({
							title:"温馨提示",
							content:res1.message+",是否现在返回首页",
							success(showRes) {
								if(showRes.confirm){
									uni.switchTab({
										url:"../index/index"
									})
								}
							}
						})
					}
					uni.hideLoading();
				})
			}
		}
	}
</script>

<style>
.doctor_message{
	width: 84%;
	margin: 0 auto;
	margin-top: 20px;
	background-color: #FFFFFF;
	padding: 20px;
	border-radius: 4px;
	text-indent: 1em;
}
.doctor_message .doctor_photo{
	float: left;
	width: 60px;
	height: 60px;
	background-color: #83B7FF;
	border-radius: 4px;
}
.medical_info{
	width: 96%;
	margin: 0 auto;
	margin-top: 10px;
	background-color: #FFFFFF;
	border-radius: 4px;
}
.medical_info li{
	color: #999999;
	width: 94%;
	margin: 0 auto;
	height: 60px;
	border-bottom: 1px solid #F5F5F5;
	line-height: 60px;
}
.medical_info li b{
	color: #000000;
	float: right;
}
.medical_info .select_icon{
	float: right;
	width: 16px;
	height: 16px;
	margin-top: 22px;
	background-image: url(../../static/124@2x.png);
	background-size: 100% 100%;
}
.sure_btn{
	width: 96%;
	margin-top: 50px;
	background-color: #3692FF;
	border-radius: 21px;
	color: #FFFFFF;
}

.update_icon{
	width: 36px;
	height: 36px;
	background-image: url(../../static/311@2x.png);
	background-size: 100% 100%;
}
.show_box_content{
	position: fixed;
	z-index:999; 
	bottom: 0px;
	height: 50%;
	background-color: #FFFFFF;
	width: 100%;
	transition: height .1s linear;
	border-radius: 10px 10px 0px 0px;
	font-size: 16px;
}
.show_box_content .title{
	margin-top: 10px;
	text-align: center;
}
.show_box_content ul{
	width: 100%;
	margin-top: 10px;
}
.show_box_content ul li{
	width: 92%;
	margin: 0 auto;
	border-bottom: 1px solid #F0F0F0;
	padding: 15px;
	display: flex;
	
}
.show_box_content ul li .person_info{
	float: left;
	width: 88%;
	line-height: 30px;
}
.show_box_content ul li .person_info p{
	color:#999999 ;
}
.show_box_content .update_icon{
	float: right;
	margin-top: 15px;
}
.show_box_content .add_person_btn{
	width: 96%;
	margin-top: 30px;
	background-color: #3692FF;
	border-radius: 21px;
	color: #FFFFFF;
}
</style>
